package SingleTon;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 38917
 * Date: 2025-03-08
 * Time: 23:31
 */
class Lazy{
    //禁止指令重排序
    private static volatile Lazy lazy = null;
    private static final Object locker = new Object();
    //保证原子性
    public static Lazy getLazy(){
        if (lazy == null) {
            synchronized (locker) {
                if (lazy == null) {
                    lazy = new Lazy();
                }
            }
        }
        return lazy;
    }

    private Lazy(){}
}

public class TestLazy {
    public static void main(String[] args) {
        Lazy lazy1 = Lazy.getLazy();
        Lazy lazy2 = Lazy.getLazy();
        System.out.println(lazy1);
        System.out.println(lazy2);
        System.out.println(lazy1 == lazy2);
    }
}
